// ==================================================================================
// Shared Genomics Project MPI Codebase
// Version 1.0 30/04/2010
//
// (c) 2010 University of Manchester all rights reserved
//
// This file is distributed under the GNU General Public License, Version 2.  
// Please see the file COPYING.txt for more details
// ==================================================================================

#ifndef _EPI_UTILS_H_
#define _EPI_UTILS_H_

#include <stdio.h>
#include "adjusted_2d.h"
#include "options.h"

#ifdef __cplusplus
extern "C" {
#endif

/*!
\file
\ingroup	gio
\brief		Utility Functions for Version 2 Epistasis programs
*/

#ifndef MAX_LINE_LENGTH
#define MAX_LINE_LENGTH 1024
#endif

#define MODE_NOT_SET 0
#define MODE_NON_PERMUTED 1
#define MODE_NON_PERMUTED_MERGE 2
#define MODE_STOP 3
#define MODE_PERMUTED 4
#define MODE_PERMUTED_MERGE 5

/*!
\brief File Clean-up routine for the Version 2 epistasis programs
\details
	The location and name of temporary file to delete are specified by the parameters
	\ref selected_options::rdir and \ref selected_options::szRunId .
\param [in] ops Input file location
\param [in] mode Epistasis mode, i.e. what temporary file to delete
\param [in] numprocs No. processors involved in a calculation
\param [in] snpMajor Flag if SNP major, i.e snpMajor != 0
\returns void
*/
void epi_utils_cleanup(struct selected_options *ops, int mode, int numprocs, int snpMajor);

/*!
\brief Write epistasis permutation counts to file.
\details
	The location of final output file is a concatonation of 
	\ref selected_options::rdir and \ref selected_options::szRunId, rank
	and \ref SUFFIX_REPI_FILE .
\param [in] ops Output file location
\param [in] numprocs No. processors involved in a calculation
\param [in] start_perm Start of permutation slice done by a single rank
\param [in] end_perm Start of permutation slice done by a single rank
\param [in] adj Adjusted p-value array for non-permuted calculation (provide interaction index co-ordinates).
\param [in] nAdj Size of the adjusted array
\param [in] R Count array for the empirical p-value
\param [in] maxR Count array for the empirical p-value
\param [in] nInts No. of permutations that an interesting interaction generated a valid test statistic
\param [in] rank Processor Number
*/
int epi_util_perm_counts_write_and_copy(struct selected_options *ops, int numprocs, int start_perm, int end_perm, struct adjusted_2d *adj, int nAdj, int *R, int *maxR, int *nInts, int rank);

/*!
\brief Read epistasis permutation counts to file.
\details
	The location of input files are a concatonation of 
	\ref selected_options::rdir and \ref selected_options::szRunId, rank
	and \ref SUFFIX_REPI_FILE .
\param [in] ops Input file location
\param [in] adj An Array of adjusted p-values of interesting interactions
\param [in] nAdj Size of the adjusted array
\param [in,out] R Destination array for the empirical p-value of interesting interactions
\param [in,out] maxR Count array for the empirical p-value
\param [in,out] nInts No. of permutations that an interesting interaction generated a valid test statistic
\param [in,out] numprocs Pointer to store the number of processors participating in a calculation
*/
int epi_util_perm_counts_read(struct selected_options *ops, struct adjusted_2d *adj, int nAdj, int *R, int *maxR, int *nInts, int *numprocs);

/*!
\brief Set the Epistasis Iterations limits
\details
	Calculates the number of pairwise interactions and interactions per processor rank.
\param [in] myrank The processor number
\param [in] nSNPs No. SNPs in the data-set.
\param [in] numprocs No. processors allocated to a calculation
\param [in,out] limits Destination array to store the interaction limits
\returns 1 on success, 0 on failure
*/
int epi_util_set_2d_limits(int myrank, int nSNPs, int numprocs, int *limits);

#ifdef __cplusplus
}
#endif

#endif // _EPI_UTILS_H_
